<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="第一分类" prop="onetype">
      <el-input v-model="dataForm.onetype" placeholder="第一分类"></el-input>
    </el-form-item>
    <el-form-item label="第二分类" prop="twotype">
      <el-input v-model="dataForm.twotype" placeholder="第二分类"></el-input>
    </el-form-item> -->
    <el-form-item label="一级分类">
      <el-select v-model="dataForm.onetype" placeholder="请选择一级分类">
        <el-option v-for="item in options1" :key="item.id" :label="item.remarks" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="二级分类">
      <el-select v-model="dataForm.twotype" placeholder="请选择二级分类" @click.native="getTow()">
        <el-option v-for="item in options2" :key="item.id" :label="item.remarks" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
   <el-form-item label="是否可用">
   <el-tooltip :content="'Switch value: ' + dataForm.enable" placement="top">
  <el-switch v-model="dataForm.enable" active-color="#13ce66" inactive-color="#ff4949" active-value="1" inactive-value="0">
  </el-switch>
</el-tooltip>
  </el-form-item>
    <el-form-item label="症状" prop="remarks">
      <el-input v-model="dataForm.remarks" placeholder="症状"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
     watch: {
    'dataForm.onetype': function(newValue, oldValue) {
      this.options2 = null;
      this.options3 = '';
      this.dataForm.threetype1 = '';
      this.dataForm.threetype2 = '';
      this.dataForm.threetype3 = '';
      this.dataForm.twotype = '';
    },
    // 'dataForm.twotype': function(newValue, oldValue) {

    //   this.options2 = null;
    //   this.options3 = '';
    //   this.dataForm.threetype1 = '';
    //   this.dataForm.threetype2 = '';
    //   this.dataForm.threetype3 = '';
    // }
  },
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          onetype: '',
          twotype: '',
          createtime: '',
          updatetime: '',
          enable: '',
          remarks: ''
        },
        options1:[],
        options2:[],
        dataRule: {
          onetype: [
            { required: true, message: '第一分类不能为空', trigger: 'blur' }
          ],
          twotype: [
            { required: true, message: '第二分类不能为空', trigger: 'blur' }
          ],
          createtime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updatetime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          enable: [
            { required: true, message: '是否显示不能为空', trigger: 'blur' }
          ],
          remarks: [
            { required: true, message: '症状不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/doctorthree/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.onetype = data.doctorThree.onetype
                this.dataForm.twotype = data.doctorThree.twotype
                this.dataForm.createtime = data.doctorThree.createtime
                this.dataForm.updatetime = data.doctorThree.updatetime
                this.dataForm.enable = data.doctorThree.enable
                this.dataForm.remarks = data.doctorThree.remarks
                this.getOne();
      this.$http({
        url: this.$http.adornUrl("/generator/doctortwo/list"),
        method: "post",
        params: this.$http.adornParams({
          onetype: this.dataForm.onetype
        })
      }).then(({
        data
      }) => {
        if (data && data.code === 0) {
          this.options2 = data.page.list;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    
              }
            })
          }
        })
      },

      getOne: function(del) {
      this.$http({
        url: this.$http.adornUrl("/generator/doctorone/list"),
        method: "post",
        params: this.$http.adornParams({})
      }).then(({
        data
      }) => {
        if (data && data.code === 0) {
          this.options1 = data.page.list;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
        return true;
      });

    },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/doctorthree/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'onetype': this.dataForm.onetype,
                'twotype': this.dataForm.twotype,
                'createtime': this.dataForm.createtime,
                'updatetime': this.dataForm.updatetime,
                'enable': this.dataForm.enable,
                'remarks': this.dataForm.remarks
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
        ,
       getTow: function(onetype) {
      this.$http({
        url: this.$http.adornUrl("/generator/doctortwo/list"),
        method: "post",
        params: this.$http.adornParams({
          onetype: this.dataForm.onetype
        })
      }).then(({
        data
      }) => {
        if (data && data.code === 0) {
          this.options2 = data.page.list;
          // this.options3 = [];
        } else {
          this.dataList = [];

          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    }
  }
</script>
